﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
using Models;

namespace 包覆门.ColorManager
{
    public partial class ColorEditing : Form
    {
        public ColorEditing()
        {
            InitializeComponent();
        }
        public string EditingId { get; set; }
        public bool IsNew { get; set; }
        private ColorDAL dal = new ColorDAL();
        private void ColorEditing_Load(object sender, EventArgs e)
        {
            if (IsNew) { } else {
                MenxingColor mc = dal.GetMenxingColorById(EditingId);
                this.tbColor.Text = mc.Mcolor;
                this.tbPrice.Text = mc.Mprice;
                foreach(RadioButton rb in this.gbgylb.Controls){
                    if(rb.Tag.ToString().Equals(mc.Gylb.ToString())){
                        rb.Checked=true;
                        break;
                    }
                }
                     foreach(RadioButton rb in this.gbold.Controls){
                    if (rb.Tag.ToString().Equals(mc.Isold.ToString()))
                    {
                        rb.Checked = true;
                        break;
                    }
                }

            }
        }
        //保存
        private void btnSave_Click(object sender, EventArgs e)
        {
            MenxingColor mc = new MenxingColor();
            mc.Mcolor = this.tbColor.Text;
            mc.Mprice = this.tbPrice.Text;
            foreach(RadioButton rb in this.gbgylb.Controls){
                if (rb.Checked) {
                    mc.Gylb = rb.Tag.ToString();
                    break;
                }
            }
            foreach (RadioButton rb in this.gbold.Controls) { 
                if(rb.Checked){
                    mc.Isold = rb.Tag.ToString();
                }
            }

            if (IsNew)
            {
                if (ValidateData(mc)) {
                    int result = dal.SaveData(mc);
                    if (result == 1)
                    {
                        MessageBox.Show("保存成功");
                        DialogResult = DialogResult.OK;
                    }
                    else {
                        MessageBox.Show("保存失败");
                        DialogResult = DialogResult.No;
                    }
                }
            }
            else
            {
                mc.Mid = Convert.ToInt32(EditingId);
                if (ValidateData(mc)) {
                    int result = dal.UpdateData(mc);
                    if (result == 1)
                    {
                        MessageBox.Show("保存成功");
                        DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show("保存失败");
                        DialogResult = DialogResult.No;
                    }
                }
            }
        }
        //取消
        private void btCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.No;
        }

        public bool ValidateData(MenxingColor mc) {
            bool flag = true;
            if(string.IsNullOrWhiteSpace(mc.Mcolor)){
                MessageBox.Show("颜色不能为空");
                return false;
            }
            if (string.IsNullOrWhiteSpace(mc.Mprice))
            {
                MessageBox.Show("价格不能为空");
                return false;
            }

            return flag;

        }

    }
}
